/*
*	Name: calendarTime - jQuery日期时间区间选择插件
*	Date: 2017-07-10
*   Contributing Author: 招摇之 <laoqianduancom@163.com - qq1096237263>
*  
*   www.laoqianduan.com 
*/
;(function($){
	'use strict';
	var calendarTime = function(options,that){
		var op = {
			type:'dateTime',
			isArea:false, 
			minDate:'1970-01-01 00:00',
			minTheDate:false,
			format:'yyyy-MM-dd hh:mm',
			inputName:'calendarTime',
			isInputValue:false,
			maxYear:100,
			isAP:false,
			isMini:true,
			appBox:'body'
		};
		op = $.extend(op,options);
		var o = new Object();
		o.startAP = 0;
		o.numberToTextArray = ['00','01','02','03','04','05','06','07','08','09'];

		var theDate = new Date();	
		o.theYear = o.showYear = o.showYearEnd = theDate.getFullYear();
		o.theMonth =  o.showMonth = o.showMonthEnd = theDate.getMonth()+1;
		o.theDay =  o.showDay = o.showDayEnd = theDate.getDate();
		o.theHours = theDate.getHours();
		o.theMinutes = theDate.getMinutes();
		o.showMonthEnd++;
		if(o.showMonthEnd>12){
			o.showYearEnd++;
		};
		if(op.minTheDate){
			op.minDate = op.minDate = o.theYear+"-"+(o.numberToTextArray[o.theMonth]||o.theMonth)+"-"+o.theDay+" "+o.theHours+":"+o.theMinutes;
		};
		o.minDateYear   = op.minDate.split(' ')[0].split('-')[0];
		o.minDateMonth  = op.minDate.split(' ')[0].split('-')[1];
		o.minDateDay    = op.minDate.split(' ')[0].split('-')[2];
		o.minDateHour   = o.minDateHourOld = op.minDate.split(' ')[1].split(':')[0];
		o.minDateMinute = op.minDate.split(' ')[1].split(':')[1];

		o.minDateUx = (new Date(op.minDate)).getTime();

		if(op.isInputValue){
			if($('input[name="'+op.inputName+'[start]'+'"]').length==0){
				that.parent().append('<input type="hidden" name="'+op.inputName+'[start]" />');
			}
			if($('input[name="'+op.inputName+'[end]'+'"]').length==0&&op.isArea){
				that.parent().append('<input type="hidden" name="'+op.inputName+'[end]" />');
			}
		};
		
		var numberToTexts = function(number){
			return o.numberToTextArray[number]||number;
		};
		that.getDateEle = function(year,month){
			var boxw = op.isMini?437:538;
			var ww = $(window).width();
			if(op.type=="time"){
				boxw = op.isMini?107:146;
			}else if(op.type=="date"){
				boxw = op.isMini?331:391;
			};
			if(op.isArea){
				boxw=boxw*2;
			};
			if(o.sx>=(boxw/2-o.sw/2)&&ww-o.sw/2-o.sx>boxw/2){
				var left = o.sx-(boxw/2-o.sw/2);
			}else if(o.sx>=boxw/2-o.sw/2){
				var left = ww-boxw-40;
			}else{
				left = 0;
			};
			if(left>o.sx){
				var arrleft = left+boxw/2-4;				
			}else{
				var arrleft = o.sx+o.sw/2-4;
			};
			

			var top = o.sy+o.sh+20;
			var style = "left:"+left+"px;top:"+top+"px;"+"width:"+boxw+"px;";
			var arrstyle ="left:"+arrleft+"px;top:"+(top-8)+"px;";
			var closestyle= "left:"+(left+boxw-7)+"px;top:"+(top-14)+"px;";
			var dateBoxEle = that.getDateBoxEle();
			var boxEle = '<div class="calendarTimeBg"></div><i class="calendarTimeArrow" style="'+arrstyle+'"></i><div class="calendarTimeBox'+(op.isArea?' area':'')+(op.isMini?" mini":"")+'" style="'+style+'">';
			boxEle+=dateBoxEle;
			boxEle+='</div><a href="javascript:;" style="'+closestyle+'" class="calendarClose">&times;</a></div>';
			return boxEle;
		};
		that.getDateBoxEle = function(){
			var boxEle = '';
			if(op.type!="time"){
				boxEle = '<div class="calendarContentDate">';
				var headEle = that.getDateHeadEle(o.showYear,o.showMonth);
				var theadEle = that.getDateTheadEle(o.showYear,o.showMonth);
				var tbodyEle = that.getDateTbodyEle(o.showYear,o.showMonth);
				
				boxEle+=headEle;
				boxEle+='<div class="calendarTimeTable">';
				boxEle+=theadEle;
				boxEle+=tbodyEle;
				boxEle+='</div></div>';
			};
				

			var timeEle = that.getTimeEle();
			if(op.type!='date'){
				boxEle+='<div class="calendarContentTime">';
				boxEle+=timeEle;
				boxEle+='</div>'
			};
			if(op.isArea){
				if(op.type!='time'){
					boxEle += '<div class="calendarContentDate borderLeft">';
					headEle = that.getDateHeadEle(o.showYearEnd,o.showMonthEnd,true);
					theadEle = that.getDateTheadEle(o.showYearEnd,o.showMonthEnd,true);
					tbodyEle = that.getDateTbodyEle(o.showYearEnd,o.showMonthEnd,true);
					
					boxEle+=headEle;
					boxEle+='<div class="calendarTimeTable">';
					boxEle+=theadEle;
					boxEle+=tbodyEle;
					boxEle+='</div></div>';
				};					
				timeEle = that.getTimeEle(true);
				if(op.type!='date'){
					boxEle+='<div class="calendarContentTime">';
					boxEle+=timeEle;
					boxEle+='</div>';
				};
			};

			return boxEle;
		};
		that.getDateHeadEle = function(year,month,area){
			var headEle = '<div class="calendarTimeHead">';
			if(area&&(o.showYearEnd>o.showYear+1||(o.showYearEnd>o.showYear&&o.showMonthEnd>=o.showMonth))){
				headEle+='<div class="'+(area?'prevYearEnd':'prevYear')+' prevYearBox isClick"><span class="arrorLeft"></span><span class="arrorLeft mr"></span></div>';
			}else if(o.showYear>o.minDateYear){
				headEle+='<div class="'+(area?'prevYearEnd':'prevYear')+' prevYearBox isClick"><span class="arrorLeft"></span><span class="arrorLeft mr"></span></div>';
			}else{
				headEle+='<div class="'+(area?'prevYearEnd':'prevYear')+' prevYearBox"><span class="arrorLeft"></span><span class="arrorLeft mr"></span></div>';
			};
			if(area&&((o.showYearEnd>o.showYear)||o.showYearEnd==o.showYear&&o.showMonthEnd>o.showMonth)){
				headEle+='<div class="'+(area?'prevMonthEnd':'prevMonth')+' prevMonthBox isClick"><span class="arrorLeft"></span></div>';
			}else if(o.showYear>o.minDateYear||(o.showYear==o.minDateYear&&o.showMonth>o.minDateMonth)){
				headEle+='<div class="'+(area?'prevMonthEnd':'prevMonth')+' prevMonthBox isClick"><span class="arrorLeft"></span></div>';
			}else{
				headEle+='<div class="'+(area?'prevMonthEnd':'prevMonth')+' prevMonthBox"><span class="arrorLeft"></span></div>';
			};
			headEle+=`						
					<div class="timeSelect">
						<label class="theSelect theYear">
							<i class="arrowBottom"></i>
							<input type="text" readonly="readonly" class="calendarSelectInput" value="`+numberToTexts(year)+`"/>
							<div class="calendarSelects">
								<div class="calendarSelectListBox calendarBarBox">
									<div class="calendarSelectList barOverflowList">
										<div class="list">
					`;
			for(var i=o.minDateYear;i<=o.theYear+op.maxYear;i++){
				headEle+= '<div class="calendarSelectOption '+(area?'yearEnd':'year')+'">'+i+'</div>';
			};
			headEle +=`
										</div>
									</div>
									<div class="calendarBar"><span class="bar"></span></div>
								</div>
							</div>
						</label>
						<label class="theSelect theYear">
							<i class="arrowBottom"></i>
							<input type="text" readonly="readonly" class="min calendarSelectInput" value="`+numberToTexts(month)+`"/>
							<div class="calendarSelects">
								<div class="calendarSelectListBox calendarBarBox">
									<div class="calendarSelectList barOverflowList">
										<div class="list">
					`;
			var minMonth = o.showYear<=o.minDateYear?o.minDateMonth:1;
			for(var i=minMonth;i<=12;i++){
				headEle+= '<div class="calendarSelectOption '+(area?'monthEnd':'month')+'">'+i+'</div>';
			};
			headEle +=`
										</div>
									</div>
									<div class="calendarBar"><span class="bar"></span></div>
								</div>
							</div>
						</label>
					</div>
					<div class="`+(area?'nextMonthEnd ':'nextMonth ')+`nextMonthBox isClick">
						<span class="arrorRight"></span>
					</div>
					<div class="`+(area?'nextYearEnd ':'nextYear ')+`nextYearBox isClick">
						<span class="arrorRight ml"></span>
						<span class="arrorRight"></span>
					</div>
				</div>
					`;

			return headEle;
		};
		that.getDateTheadEle = function(){
			var theadEle = '<div class="calendarTimeThead"><div class="th">日</div><div class="th">一</div><div class="th">二</div><div class="th">三</div><div class="th">四</div><div class="th">五</div><div class="th">六</div></div>';
			return theadEle;
		};
		that.getDateTbodyEle = function(year,month,area){

			var bodyDate  = new Date(year+"-"+month+"-1");
			var startWeek = bodyDate.getDay();

			bodyDate.setMonth(month);
        	bodyDate.setDate(0);
			var dayCount  = bodyDate.getDate();

			var timeStamp= (new Date(year+"-"+month+"-1")).valueOf();
			var tbodyEle = '<div class="calendarTimeTbody">';
				for(var i=startWeek;i>0;i--){
					var newTimeStamp = timeStamp-86400000*i;
					var newDate = new Date(newTimeStamp);
					var newDay = newDate.getDate();
					tbodyEle+='<div class="td disabled" data-date="'+newDate.getFullYear()+'-'+numberToTexts(newDate.getMonth()+1)+'-'+numberToTexts(newDay)+'">'+numberToTexts(newDay)+'</div>';
				};
				for(var i=1;i<=dayCount;i++){
					var selectDate = (new Date(o.selectedYear+"-"+o.selectedMonth+"-"+o.selectedDay)).getTime();	
					var theDate = (new Date(year+"-"+month+"-"+i)).getTime();	
					var selectDateEnd = (new Date(o.selectedYearEnd+"-"+o.selectedMonthEnd+"-"+o.selectedDayEnd)).getTime();	
					
					if(op.isArea&&!o.selectedYearEnd&&(year<o.selectedYear||(year==o.selectedYear&&month<o.selectedMonth)||(year==o.selectedYear&&month==o.selectedMonth&&i<o.selectedDay))){
						tbodyEle+='<div class="td disabled" data-date="'+year+'-'+numberToTexts(month)+'-'+numberToTexts(i)+'">'+numberToTexts(i)+'</div>';
					}else if(theDate<o.minDateUx-86400000){
						tbodyEle+='<div class="td disabled" data-date="'+year+'-'+numberToTexts(month)+'-'+numberToTexts(i)+'">'+numberToTexts(i)+'</div>';
					}else if(theDate>=selectDate&&theDate<=selectDateEnd){
						tbodyEle+='<div class="td select selected" data-date="'+year+'-'+numberToTexts(month)+'-'+numberToTexts(i)+'">'+numberToTexts(i)+'</div>';
					}else if(o.selectedDay==i&&year==o.selectedYear&&month==o.selectedMonth){
						tbodyEle+='<div class="td select selected" data-date="'+year+'-'+numberToTexts(month)+'-'+numberToTexts(i)+'">'+numberToTexts(i)+'</div>';
					}else if(o.theDay==i&&year==o.theYear&&month==o.theMonth){
						tbodyEle+='<div class="td select theDay" data-date="'+year+'-'+numberToTexts(month)+'-'+numberToTexts(i)+'">'+numberToTexts(i)+'</div>';
					}else{
						tbodyEle+='<div class="td select" data-date="'+year+'-'+numberToTexts(month)+'-'+numberToTexts(i)+'">'+numberToTexts(i)+'</div>';
					};				
				};
				var remainCount = 42-(dayCount+startWeek);
				var lastTimeStamp= (new Date(year+"-"+month+"-"+dayCount)).valueOf();
				for(var i=1;i<=remainCount;i++){
					var newTimeStamp = lastTimeStamp+86400000*i;
					var newDate = new Date(newTimeStamp);
					var newDay = newDate.getDate();				
					tbodyEle += '<div class="td disabled" data-date="'+newDate.getFullYear()+'-'+numberToTexts(newDate.getMonth()+1)+'-'+numberToTexts(newDay)+'">'+numberToTexts(newDay)+'</div>';
				};

			tbodyEle+='</div>';
			return tbodyEle;
		};
		that.getTimeEleBox = function(area){
			var Ele =`
					<div class="timeListBoxs">
						<div class="timeListBox hour">
							<div class="timeLists calendarBarBox">
								<div class="timeList barOverflowList">
									<div class="list `+(area?' hourListEnd':' hourList')+`">
			`;
			Ele+=that.getHoursEle(area);

			Ele+=`						
									</div>
								</div>
								<div class="calendarBar"><span class="bar"></span></div>
							</div>
						</div>
						<div class="timeinterval">
							<span>:</span><span>:</span><span>:</span><span>:</span><span>:</span><span>:</span><span>:</span><span>:</span><span>:</span><span>:</span>
						</div>
						<div class="timeListBox">
							<div class="timeLists calendarBarBox">
								<div class="timeList barOverflowList">
									<div class="list `+(area?' minuteListEnd':' minuteList')+`">
			`;
			Ele+=that.getMinuteEle(area);
			
			Ele+=`						
									</div>
								</div>
								<div class="calendarBar"><span class="bar"></span></div>
							</div>
						</div>
			`;
			return Ele;
		};
		that.getTimeEle = function(area){
			var name = (area?'calendarTimeRadioEnd':'calendarTimeRadio');
			var Ele = `
				
					<div class="checkboxAPBox`+(op.isAP?' AP':'')+`">
						<label class="checkboxAP">
							<input type="checkbox" `+(op.isAP?'':'checked="checked"')+` name="checkboxAP" />
							<div class="check">
								<span class="white">24</span>
								<span>24</span>
								<i class="icon"></i>
							</div>
						</label>
						<label class="radioBox radioBoxA">
							<input type="radio" `+(o.startAP==0?'checked="true"':'')+` name="`+name+`" value="0"/>
							<span>A</span>
						</label>
						<label class="radioBox radioBoxP">
							<input type="radio" `+(o.startAP==1?'checked="true"':'')+` name="`+name+`" value="1"/>
							<span>P</span>
						</label>
					</div>
					<div class="timeHourMinuteBox">
				`;
			Ele+=that.getTimeEleBox(area);
			Ele+=`</div>
					</div>

			`;
			return Ele;
		};
		that.getHoursEle = function(area){
			var hourEle = "";
			var maxHour = 24;
			if(op.isAP){
				maxHour = 12;
			};
			var start = 0;
			if(o.selectedYear&&area&&o.selectedYear==o.selectedYearEnd&&o.selectedMonth==o.selectedMonthEnd&&o.selectedDay==o.selectedDayEnd){
				start = o.selectedHours;
			}else if((o.selectedYear&&o.selectedYear==o.minDateYear&&o.selectedMonth==o.minDateMonth&&o.selectedDay==o.minDateDay)||op.type=="time"){
				start = o.minDateHour;
			};
			for(var i=0;i<maxHour;i++){
				if(i<start){
					hourEle += '<div class="calendarSelectOption disabled">'+(o.numberToTextArray[i]||i)+'</div>';
				}else if(area&&i==o.selectedHoursEnd){
					hourEle += '<div class="calendarSelectOption '+(area?'hourEnd':'hour')+' selected">'+(o.numberToTextArray[i]||i)+'</div>';
				}else if(!area&&i==o.selectedHours){
					hourEle += '<div class="calendarSelectOption '+(area?'hourEnd':'hour')+' selected">'+(o.numberToTextArray[i]||i)+'</div>';
				}else{
					hourEle += '<div class="calendarSelectOption '+(area?'hourEnd':'hour')+'">'+(o.numberToTextArray[i]||i)+'</div>';
				};
				
			};
			return hourEle;
		};
		that.getMinuteEle = function(area){
			var minuteEle = "";
			var maxMinute = 60;
			var start = 0;
			if(o.selectedHours>=0&&o.selectedYear&&area&&o.selectedYear==o.selectedYearEnd&&o.selectedMonth==o.selectedMonthEnd&&o.selectedDay==o.selectedDayEnd&&o.selectedHours==o.selectedHoursEnd){
				start = o.selectedMinutes;
			}else if((o.selectedHours>=0&&o.selectedYear&&o.selectedYear==o.minDateYear&&o.selectedMonth==o.minDateMonth&&o.selectedDay==o.minDateDay&&o.selectedHours==o.minDateHour)||(op.type=="time"&&o.selectedHours==o.minDateHour)){
				start = o.minDateMinute;
			};
			for(var i=0;i<maxMinute;i++){
				if(i<start){
					minuteEle += '<div class="calendarSelectOption disabled">'+(o.numberToTextArray[i]||i)+'</div>';
				}else if(area&&i==o.selectedMinutesEnd){
					minuteEle += '<div class="calendarSelectOption '+(area?'minuteEnd':'minute')+' selected">'+(o.numberToTextArray[i]||i)+'</div>';
				}else if(!area&&i==o.selectedMinutes){
					minuteEle += '<div class="calendarSelectOption '+(area?'minuteEnd':'minute')+' selected">'+(o.numberToTextArray[i]||i)+'</div>';
				}else{
					minuteEle += '<div class="calendarSelectOption '+(area?'minuteEnd':'minute')+'">'+(o.numberToTextArray[i]||i)+'</div>';
				}
			};
			return minuteEle;
		};				
		that.SwitchAP = function(){			
			if($('.calendarTimeBox [name="calendarTimeRadio"]:checked').val()==1){
				if(o.minDateHour>11){
					o.minDateHour-=12;	
				};			
			}else{
				o.minDateHour=o.minDateHourOld;
			};
			var html = that.getHoursEle();	
			var hhtml = that.getHoursEle(true);
			$('.calendarTimeBox .hourList').html(html);
			$('.calendarTimeBox .hourListEnd').html(hhtml);
			that.setInputDate();
		};
		that.setInputDate = function(){
			var sAP = "";
			var eAP = "";
			if(op.isAP){
				if($('.calendarTimeBox [name="calendarTimeRadio"]:checked').val()==0){
					sAP += " AM";
				}else{
					sAP += " PM";
				}
				if($('.calendarTimeBox [name="calendarTimeRadioEnd"]:checked').val()==0){
					eAP += " AM";
				}else{
					eAP += " PM";
				}
			};

			var dataValue="";
			if(op.type!='time'){
				dataValue += (o.selectedYear?(numberToTexts(o.selectedYear)):"xxxx")+'-'+(o.selectedMonth?(numberToTexts(o.selectedMonth)):"xx")+'-'+(o.selectedDay?(numberToTexts(o.selectedDay)):"xx")
			};
			
			if(op.type!="date"){
				dataValue+=' '+((o.selectedHours||o.selectedHours==0)?(numberToTexts(o.selectedHours)):"xx")+':'+((o.selectedMinutes||o.selectedMinutes==0)?(numberToTexts(o.selectedMinutes)):"xx")+sAP;
			};

			if(op.isArea){
				var dataValueEnd="";
				if(op.type!='time'){
					dataValueEnd += (o.selectedYearEnd?(numberToTexts(o.selectedYearEnd)):"xxxx")+'-'+(o.selectedMonthEnd?(numberToTexts(o.selectedMonthEnd)):"xx")+'-'+(o.selectedDayEnd?(numberToTexts(o.selectedDayEnd)):"xx");
				};
				if(op.type!="date"){
					dataValueEnd+=' '+((o.selectedHoursEnd||o.selectedHoursEnd==0)?(numberToTexts(o.selectedHoursEnd)):"xx")+':'+((o.selectedMinutesEnd||o.selectedMinutesEnd==0)?(numberToTexts(o.selectedMinutesEnd)):"xx")+eAP;
				};
			}else if(op.type=="date"&&o.selectedYear){
				$('.calendarTimeBg,.calendarClose,.calendarTimeArrow,.calendarTimeBox').remove();
				if(op.selected){
					op.selected();
				};
			}else if(op.type=="time"&&(o.selectedMinutes||o.selectedMinutes==0)&&(o.selectedHours||o.selectedHours==0)){
				$('.calendarTimeBg,.calendarClose,.calendarTimeArrow,.calendarTimeBox').remove();
				if(op.selected){
					op.selected();
				};				
			};

			
			if(op.isInputValue){
				$('input[name="'+op.inputName+'[start]'+'"]').val(dataValue);
				if(op.isArea){
					$('input[name="'+op.inputName+'[end]'+'"]').val(dataValueEnd);
				};			
			};
			if(op.isArea){
				that.val(dataValue+' / '+dataValueEnd);
			}else{
				that.val(dataValue);
			};


		};


		// bar
		that.barInit = function(){
			$('.calendarTimeBox .calendarBar').each(function(index){
				var that = $(this);
				var h = $(this).parent().outerHeight();
				var xh = $(this).parent().find('.list').outerHeight();
				if(xh<=h){
					$(this).remove();
				};
				var barH = h/xh*h;
				$(this).find('.bar').height(barH);
				$(this).parent().find('.barOverflowList').scroll(function(){
					that.find('.bar').css('top',$(this).scrollTop()/(xh-h)*(h-barH)+"px");
				});

				var selectedval = $(this).parents('.calendarSelects').parent().find('.calendarSelectInput').val();
				$(this).parents('.calendarSelects').find('.calendarSelectOption').each(function(index){
					if($(this).text()==selectedval){
						var top = index/$(this).parents('.calendarSelects').find('.calendarSelectOption').length;
						that.parent().find('.barOverflowList').scrollTop(parseInt(top*xh-h/2));
						that.find('.bar').css('top',parseInt((top*xh-h/2)/xh*100)+"%");
					};
				});

				var pageY = 0;
				var isDown = false;
				var barTop = 0;
				$(this).find('.bar').mousedown(function(e){
					pageY = e.pageY;
					barTop = parseInt($(this).css('top'));
					isDown = true;
				});
				$(document).mousemove(function(e) {
					if(isDown){
						var newTop = e.pageY-pageY+barTop;
						if(newTop<0){
							newTop=0;
						}else if(newTop>h-barH){
							newTop = h-barH;
						};
						that.find('.bar').css('top',newTop+"px");
						that.parent().find('.barOverflowList').scrollTop(newTop/(h-barH)*(xh-h));
					};
					
				});
				$(document).mouseup(function(e) {
					isDown = false;
				});
			})
		};
		that.on('focus',function(){
			o.sx = that.offset().left;
		    o.sy = that.offset().top;
		    o.sw = that.outerWidth();
		    o.sh = that.outerHeight();
		    

			var htmlEle = that.getDateEle();
			$(op.appBox).append(htmlEle);

			that.barInit();
			$('.calendarTimeBg,.calendarClose').click(function(){$('.calendarTimeBg,.calendarClose,.calendarTimeArrow,.calendarTimeBox').remove()});
			$('.calendarTimeBox').on('click','.prevYear.isClick',function(){
				o.showYear--;
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);
				that.barInit();
			});
			$('.calendarTimeBox').on('click','.nextYear.isClick',function(){
				o.showYear++;
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);
				that.barInit();
			});
			$('.calendarTimeBox').on('click','.nextMonth.isClick',function(){
				o.showMonth++;
				if(o.showMonth>12){
					o.showMonth=1;
					o.showYear++;
				};
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);
				that.barInit();
			});
			$('.calendarTimeBox').on('click','.prevMonth.isClick',function(){
				o.showMonth--;
				if(o.showMonth<=0){
					o.showMonth=12;
					o.showYear--;
				};
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);
				that.barInit();
			});

			$('.calendarTimeBox').on('click','.prevYearEnd.isClick',function(){
				o.showYearEnd--;
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);
				that.barInit();
			});
			$('.calendarTimeBox').on('click','.nextYearEnd.isClick',function(){
				o.showYearEnd++;
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);
				that.barInit();
			});
			$('.calendarTimeBox').on('click','.nextMonthEnd.isClick',function(){
				o.showMonthEnd++;
				if(o.showMonthEnd>12){
					o.showMonthEnd=1;
					o.showYearEnd++;
				}
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);
				that.barInit();
			});
			$('.calendarTimeBox').on('click','.prevMonthEnd.isClick',function(){
				o.showMonthEnd--;
				if(o.showMonthEnd<=0){
					o.showMonthEnd=12;
					o.showYearEnd--;
				};
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);
				that.barInit();
			});

			$('.calendarTimeBox').on('click','.td.select',function(){
				if(!o.selectedYearEnd&&o.selectedYear&&op.isArea){
					o.selectedYearEnd = parseInt($(this).data('date').split('-')[0]);		
					o.selectedMonthEnd = parseInt($(this).data('date').split('-')[1]);		
					o.selectedDayEnd = parseInt($(this).data('date').split('-')[2]);	
				}else{
					o.selectedYearEnd = null;
					o.selectedMonthEnd = null;
					o.selectedDayEnd = null;
					o.selectedYear = parseInt($(this).data('date').split('-')[0]);		
					o.selectedMonth = parseInt($(this).data('date').split('-')[1]);		
					o.selectedDay = parseInt($(this).data('date').split('-')[2]);	
				};		
				that.setInputDate();
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);	
				that.barInit();
			});
			$('.calendarTimeBox').on('mousemove','.td.select',function(){			
				if(op.isArea&&o.selectedYear&&!o.selectedYearEnd){
					$('.calendarTimeBox .td.select').removeClass('hover');
					var theDate = (new Date($(this).data('date'))).getTime();
					var selectDate = (new Date(o.selectedYear+"-"+o.selectedMonth+"-"+o.selectedDay)).getTime();	
					$('.calendarTimeBox .td.select').each(function(){
						var hdate = (new Date($(this).data('date'))).getTime();	
						if(hdate<=theDate&&hdate>=selectDate){
							$(this).addClass('hover');
						};
					});
				};
			});
			$('.calendarTimeBox').on('click','.calendarSelectOption.hour',function(){
				o.selectedHours = parseInt($(this).text());
				var html = that.getMinuteEle();	
				$(this).addClass('selected').siblings().removeClass('selected');		
				$(this).parents('.calendarTimeBox').find('.minuteList').html(html);

				var hhtml = that.getHoursEle(true);
				var mhtml = that.getMinuteEle(true);
				$(this).parents('.calendarTimeBox').find('.hourListEnd').html(hhtml);
				$(this).parents('.calendarTimeBox').find('.minuteListEnd').html(mhtml);

				that.setInputDate();
			});
			$('.calendarTimeBox').on('click','.calendarSelectOption.minute',function(){
				o.selectedMinutes = parseInt($(this).text());
				$(this).addClass('selected').siblings().removeClass('selected');		

				var mhtml = that.getMinuteEle(true);
				$(this).parents('.calendarTimeBox').find('.minuteListEnd').html(mhtml);
				that.setInputDate();
			});

			$('.calendarTimeBox').on('click','.calendarSelectOption.hourEnd',function(){
				o.selectedHoursEnd = parseInt($(this).text());
				$(this).addClass('selected').siblings().removeClass('selected');		

				var mhtml = that.getMinuteEle(true);
				$(this).parents('.calendarTimeBox').find('.minuteListEnd').html(mhtml);
				that.setInputDate();
			});
			$('.calendarTimeBox').on('click','.calendarSelectOption.minuteEnd',function(){
				o.selectedMinutesEnd = parseInt($(this).text());
				$(this).addClass('selected').siblings().removeClass('selected');		
				that.setInputDate();
			});


			$('.calendarTimeBox').on('click','.calendarSelectInput',function(){
				$(this).parent().find('.calendarSelects').toggleClass('show');
			});
			$('.calendarTimeBox').on('blur','.calendarSelectInput',function(){
				var thast = $(this);
				setTimeout(function(){
					thast.parent().find('.calendarSelects').removeClass('show');
				},200);			
			});
			$('.calendarTimeBox').on('click','.calendarSelects .calendarSelectOption.year',function(){
				o.showYear = parseInt($(this).text());
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);	
				that.barInit();
			});
			$('.calendarTimeBox').on('click','.calendarSelects .calendarSelectOption.month',function(){
				o.showMonth = parseInt($(this).text());
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);
				that.barInit();
			});
			$('.calendarTimeBox').on('click','.calendarSelects .calendarSelectOption.yearEnd',function(){
				o.showYearEnd = parseInt($(this).text());
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);	
				that.barInit();
			});
			$('.calendarTimeBox').on('click','.calendarSelects .calendarSelectOption.monthEnd',function(){
				o.showMonthEnd = parseInt($(this).text());
				var htmlEle = that.getDateBoxEle();
				$('.calendarTimeBox').html(htmlEle);	
				that.barInit();
			});
			$('.calendarTimeBox').on('change','[name="checkboxAP"]',function(){
				if($(this).is(':checked')){
					op.isAP = false;
					if($('.calendarTimeBox [name="calendarTimeRadio"]:checked').val()==1){
						o.selectedHours += 12;
					};
					$('.calendarTimeBox [name="checkboxAP"]').parents('.checkboxAPBox').removeClass('AP');
					$('.calendarTimeBox [name="checkboxAP"]').prop('checked',true);
				}else{
					op.isAP = true;
					if(o.selectedHours>11){
						o.selectedHours-=12;
						o.startAP = 1;
					}else{
						o.startAP = 0;
					};
					$('.calendarTimeBox [name="checkboxAP"]').parents('.checkboxAPBox').addClass('AP');
					$('.calendarTimeBox [name="checkboxAP"]').prop('checked',false);
									
				};
				
				var htmlEle = that.getTimeEleBox();		
				$('.calendarTimeBox .timeHourMinuteBox').eq(0).html(htmlEle);
				if(op.isArea){
					var htmlEleEnd= that.getTimeEleBox(true);		
					$('.calendarTimeBox .timeHourMinuteBox').eq(1).html(htmlEleEnd);
				}
				that.setInputDate();
				that.barInit();
			});
			$('.calendarTimeBox').on('click','[name="calendarTimeRadio"]',function(){
				that.SwitchAP();
			});
			$('.calendarTimeBox').on('click','[name="calendarTimeRadioEnd"]',function(){
				that.SwitchAP();
			});
		});
	};
	$.fn.calendarTime = function(op){
		calendarTime(op,$(this));
	};
})(jQuery)